#include "mainwindow.h"
#include "GameBoardView.h"
#include "ui_mainwindow.h"
#include <ctime>
#include <cstdlib>
#include <QTimer>
#include <QDebug>
#include <QMessageBox>
#include "GameBoard.h"
#include "GameBoardRendererThread.h"
#include "GameBoardSolver.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    mRowInc(false)
{
  srand(time(0));

  ui->setupUi(this);
  ui->applySolverBtn->setDisabled(false);
  addBoardBtnClicked();

  connect(ui->addBoardBtn, SIGNAL(clicked()), this, SLOT(addBoardBtnClicked()));
  connect(ui->solveBtn, SIGNAL(clicked()), this, SLOT(solveBtnClicked()));
  connect(ui->applySolverBtn, SIGNAL(clicked()), this, SLOT(applySolverBtnClicked()));
}

MainWindow::~MainWindow()
{
  delete ui;
}

void MainWindow::addBoardBtnClicked()
{
  int row = ui->rowPos->value(),
      col = ui->colPos->value();
  while (row >= mGames.count()) mGames.push_back(QList<GameSolverTuple>());
  while (col >= mGames[row].count()) mGames[row].push_back(GameSolverTuple());
  if (ui->useSameBoard->isChecked() && (row !=0 || col != 0))
    mGames[row][col].mBoard = new GameBoard(*(mGames[0][0].mBoard));
  else
    mGames[row][col].mBoard = new GameBoard(ui->numRows->value(), ui->numCols->value(), ui->numColors->value());
  mGames[row][col].mView = new GameBoardView(mGames[row][col].mBoard, ui->playable->isChecked(), this);
  mGames[row][col].mSolver = new GameBoardSolver(mGames[row][col].mBoard);
  connect(mGames[row][col].mSolver, SIGNAL(done(bool)), this, SLOT(solved(bool)));
  ui->gameBoardViewLayout->addWidget(mGames[row][col].mView, row, col, 1, 1);
  mGames[row][col].mView->startRendering();


  if (mRowInc) ui->rowPos->stepBy(1);
  else ui->colPos->stepBy(1);
  if (mRowInc && ui->rowPos->value() > ui->colPos->value())
  {
    mRowInc = !mRowInc;
    ui->colPos->setValue(0);
  }
  else if (!mRowInc && ui->colPos->value() >= ui->rowPos->value())
  {
    mRowInc = !mRowInc;
    ui->rowPos->setValue(0);
  }
  static bool first = true;
  if (ui->rowPos->value() == ui->colPos->value() && ui->rowPos->value() == 1 && first)
  {
    mRowInc = !mRowInc;
    ui->colPos->setValue(0);
    first = false;
  }
}

void MainWindow::solveBtnClicked()
{
  foreach (QList<GameSolverTuple> i, mGames)
    foreach (GameSolverTuple j, i)
      if (j.mView->selected())
      {
        j.mView->setSelected(false);
        j.mSolver->start();
      }
}

void MainWindow::applySolverBtnClicked()
{
  foreach (QList<GameSolverTuple> l, mGames)
    foreach(GameSolverTuple g, l)
      if (g.mView->selected())
      {
      }
}

void MainWindow::solved(bool solved)
{
  GameBoardSolver *solver = dynamic_cast<GameBoardSolver*>(sender());
  if (solver)
  {
    if (solved)
    {
      QMessageBox::question(this, "Solved", QString("%1 Backtracks.\n%2 Choices").arg(solver->getNumBackTracks()).arg(solver->getChoices().size()));
      foreach (GameBoard::Choice c, solver->getChoices())
        qDebug() << QPoint(c.mColumn, c.mRow);
    }
    else
      QMessageBox::question(this, "Unsolvable", QString("With %1 Backtracks.").arg(solver->getNumBackTracks()));
  }
}
